﻿using Direct.IDAL.Manager.UserCenter;
using DirectSell.Common.Url;
using DirectSell.Model.Query;
using DirectSell.Model.User;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.Manager.UserCenter
{
    /// <summary>
    /// 升级审核仓储层
    /// </summary>
    public class UpgradeAuditRepository:BaseManagerRepository, IUpgradeAuditRepository
    {
        #region 查询升级申请列表
        /// <summary>
        /// 查询升级申请列表
        /// </summary>
        public async Task<PagedList<UserUpgrade>> QueryList(BaseCondition condition)
        {
            PageParam param = new PageParam();
            param.TableName = "UserUpgrade";
            param.PageIndex = condition.pageIndex == 0 ? 1 : condition.pageIndex;
            param.PageSize = condition.pageSize;
            param.OrderBy = "UpgradeDate DESC";
            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1 ");
            if (condition.SerachCondition != null)
            {
                sb.AppendFormat("AND ( UserCode  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            }

            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND UpgradeDate>='{0}' and UpgradeDate<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND UpgradeDate>='{0}' and UpgradeDate<='{1}' ", condition.startTime, DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND UpgradeDate>='2010-01-01' and UpgradeDate<='{1}' ", condition.endTime);
                }
            }
           // sb.Append(" ORDER BY UpgradeDate DESC");
            param.StrWhere = sb.ToString();
           return await base.PageQuery<UserUpgrade>(param);
        }
        #endregion

        #region 改审核状态
        /// <summary>
        /// 改审核状态 
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public void UpdateStates(int id,int type,ref Hashtable hstable)
        {
            string str = string.Format("UPDATE UserUpgrade SET UpgradeState={0} WHERE Id={1}", type,id);
            hstable.Add(str, null);
        }
        #endregion

        #region 会员升级
        /// <summary>
        /// 会员升级
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="level"></param>
        public void Uplevel(string usercode,int level,ref Hashtable hstable)
        {
            string str = string.Format(" SELECT LevelName FROM MemberLevel WHERE LevelID={0} ", level);
            string levelName = base.QueryFirstT<string>(str);
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations SET ");
            sb.AppendFormat(" LevelName='{0}' ", levelName);
            sb.AppendFormat(" ,UserLevel={0}  ",level);
            sb.AppendFormat(" WHERE UserCode='{0}'",usercode);
            hstable.Add(sb.ToString(), null);
        }
        #endregion

    }
}
